package com.sg.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sg.entity.Dzwl;
import com.sg.entity.PointInfo;
import com.sg.entity.UserTag;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.mapper.IntegralFlowMapper;
import com.sg.entity.IntegralFlow;

import javax.annotation.Resource;

@Service
public class IntegralFlowService extends ServiceImpl<IntegralFlowMapper, IntegralFlow> {
    @Resource
    private IntegralFlowMapper integralFlowMapper;

    @Resource
    private UserTagService userTagService;
    @Resource
    private PointInfoService pointInfoService;

    public void saveFlow(Dzwl dzwl) {
        if(dzwl.getInOut() == "false"){
            return;
        }
        LambdaQueryWrapper<UserTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserTag::getStatus, 1).eq(UserTag::getTagId, dzwl.getTagId());
        UserTag one = userTagService.getOne(wrapper);
        if(one == null){
            return;
        }
        LambdaQueryWrapper<IntegralFlow> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(IntegralFlow::getTagId, dzwl.getTagId()).eq(IntegralFlow::getUserId, one.getUserId());
        List<IntegralFlow> list = list(wrapper1);
        if(!list.isEmpty()){
            return;
        }
        LambdaQueryWrapper<PointInfo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(PointInfo::getInfoId, dzwl.getInfo());
        PointInfo one1 = pointInfoService.getOne(wrapper2);
        if(one1 == null){
            return;
        }
        IntegralFlow integralFlow = new IntegralFlow();
        integralFlow.setArriveInfo(dzwl.getInfo());
        integralFlow.setChangTime(new Date());
        integralFlow.setTagId(dzwl.getTagId());
        integralFlow.setChangReason("游客到达"+one1.getName());
        integralFlow.setChangUnm(+one1.getIntegral());
        integralFlow.setUserId(one.getUserId());
        save(integralFlow);
    }

    public String getPm(Integer uderId) {
        return integralFlowMapper.getPm(uderId);
    }
}
